home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / hypercrd / xcmd / dxcmds34.sit / Dartmouth XCMD's 3.4.3 / card_19348.txt < prev    next >
Text File  |  1990-04-17  |  14KB  |  496 lines

  1. -- card: 19348 from stack: in.3
  2. -- bmap block id: 19513
  3. -- flags: 4000
  4. -- background id: 8327
  5. -- name: FindInField
  6. ----- HyperTalk script -----
  7. on Install
  8.   put ChooseTargetStack() into it
  9.   InstallResource XFCN,FindInField,it
  10. end Install
  11.  
  12. on openCard
  13.   set the hilite of btn "foundit" to false
  14.   pass openCard
  15. end openCard
  16.  
  17. on showIt pos,theLength
  18.   set the hilite of btn "foundit" to false
  19.   if item 1 of pos + item 2 of pos + item 3 of pos + item 4 of pos = 0 then
  20.     answer "Target not found!"
  21.     exit ShowIt
  22.   end if
  23.   put item 1 of the rect of card field 1 into x
  24.   put item 2 of the rect of card field 1 into y
  25.   put item 3 of pos into theChar
  26.   put item 2 of pos into theLine
  27.   put the textHeight of card field 1 into th
  28.   put 6 into ts  -- courier
  29.   put y+(theLine*th) into y
  30.   put the scroll of card field 1 into theScroll
  31.   get the rect of card field 1
  32.   put item 4 of it - item 2 of it into theHeight
  33.   if (theScroll+theHeight) < y then
  34.     set the scroll of card field 1 to (y-10*th)
  35.   end if
  36.   put x+(theChar*ts) into x
  37.   put y - the scroll of card field 1 into y
  38.   put x into item 1 of r
  39.   put x+theLength*ts+5 into item 3 of r
  40.   put y - round(th/2) into item 2 of r
  41.   put y + round(th/2) into item 4 of r
  42.   set the rect of btn "foundit" to r
  43.   set the hilite of btn "foundit" to true
  44.   wait for 1 seconds
  45. end showIt
  46.  
  47.  
  48. -- part 5 (field)
  49. -- low flags: 01
  50. -- high flags: 2007
  51. -- rect: left=18 top=32 right=285 bottom=384
  52. -- title width / last selected line: 0
  53. -- icon id / first selected line: 0 / 0
  54. -- text alignment: 0
  55. -- font id: 22
  56. -- text size: 10
  57. -- style flags: 0
  58. -- line height: 13
  59. -- part name: Documentation
  60.  
  61.  
  62. -- part 6 (button)
  63. -- low flags: 00
  64. -- high flags: 8003
  65. -- rect: left=299 top=298 right=320 bottom=438
  66. -- title width / last selected line: 0
  67. -- icon id / first selected line: 0 / 0
  68. -- text alignment: 1
  69. -- font id: 0
  70. -- text size: 12
  71. -- style flags: 0
  72. -- line height: 16
  73. -- part name: Show C Source
  74. ----- HyperTalk script -----
  75. on mouseUp
  76.   get the visible of card field "source"
  77.   set the visible of card field "source" to not it
  78.   if it is false then
  79.     set the name of me to "Hide C Source"
  80.   else
  81.     set the name of me to "Show C Source"
  82.   end if
  83. end mouseUp
  84.  
  85.  
  86.  
  87. -- part 9 (field)
  88. -- low flags: 01
  89. -- high flags: 0004
  90. -- rect: left=389 top=268 right=289 bottom=483
  91. -- title width / last selected line: 0
  92. -- icon id / first selected line: 0 / 0
  93. -- text alignment: 0
  94. -- font id: 3
  95. -- text size: 12
  96. -- style flags: 0
  97. -- line height: 16
  98. -- part name: found
  99.  
  100.  
  101. -- part 10 (button)
  102. -- low flags: 00
  103. -- high flags: 2000
  104. -- rect: left=395 top=76 right=111 bottom=480
  105. -- title width / last selected line: 0
  106. -- icon id / first selected line: 0 / 0
  107. -- text alignment: 1
  108. -- font id: 0
  109. -- text size: 12
  110. -- style flags: 0
  111. -- line height: 16
  112. -- part name: Not case
  113. ----- HyperTalk script -----
  114. on mouseUp
  115.   ask "Find what?"
  116.   put it into myTarget
  117.   if myTarget is not empty then
  118.     set the scroll of card field 1 to 0
  119.     get FindInField(card field 1,myTarget,"true",0)
  120.     put it into card field "found"
  121.     showIt it,the number of chars in myTarget
  122.   end if
  123. end mouseUp
  124.  
  125.  
  126.  
  127. -- part 11 (button)
  128. -- low flags: 00
  129. -- high flags: 2000
  130. -- rect: left=395 top=123 right=158 bottom=480
  131. -- title width / last selected line: 0
  132. -- icon id / first selected line: 0 / 0
  133. -- text alignment: 1
  134. -- font id: 0
  135. -- text size: 12
  136. -- style flags: 0
  137. -- line height: 16
  138. -- part name: New Button
  139. ----- HyperTalk script -----
  140. on mouseUp
  141.   ask "Find what?"
  142.   put it into myTarget
  143.   if myTarget is not empty then
  144.     set the scroll of card field 1 to 0
  145.     get FindInField(card field 1,myTarget,"",0)
  146.     put it into card field "found"
  147.     showIt it,the number of chars in myTarget
  148.   end if
  149. end mouseUp
  150.  
  151.  
  152.  
  153. -- part 12 (button)
  154. -- low flags: 00
  155. -- high flags: 2000
  156. -- rect: left=395 top=169 right=215 bottom=480
  157. -- title width / last selected line: 0
  158. -- icon id / first selected line: 0 / 0
  159. -- text alignment: 1
  160. -- font id: 0
  161. -- text size: 12
  162. -- style flags: 0
  163. -- line height: 16
  164. -- part name: New Button
  165. ----- HyperTalk script -----
  166. on mouseUp
  167.   ask "Show what?"
  168.   if it is not empty then
  169.     hide message
  170.     set the scroll of card field 1 to 0
  171.     put it into target
  172.     put false into done
  173.     put 0 into start
  174.     --put empty into msg
  175.     repeat while done is false
  176.       get FindInField(card field 1,target,"",start)
  177.       if item 1 of it is 0 then
  178.         put true into done
  179.       else
  180.         put it into card field "found"
  181.         showIt it,the number of chars in target
  182.         put item 1 of it + 1 into start
  183.       end if
  184.     end repeat
  185.   end if
  186. end mouseUp
  187.  
  188.  
  189.  
  190.  
  191. -- part 14 (button)
  192. -- low flags: 00
  193. -- high flags: 0000
  194. -- rect: left=395 top=46 right=64 bottom=468
  195. -- title width / last selected line: 0
  196. -- icon id / first selected line: 0 / 0
  197. -- text alignment: 1
  198. -- font id: 0
  199. -- text size: 12
  200. -- style flags: 0
  201. -- line height: 16
  202. -- part name: New Button
  203.  
  204.  
  205. -- part 7 (field)
  206. -- low flags: 81
  207. -- high flags: 0007
  208. -- rect: left=18 top=31 right=290 bottom=489
  209. -- title width / last selected line: 0
  210. -- icon id / first selected line: 0 / 0
  211. -- text alignment: 0
  212. -- font id: 3
  213. -- text size: 10
  214. -- style flags: 0
  215. -- line height: 13
  216. -- part name: Source
  217.  
  218.  
  219. -- part 16 (button)
  220. -- low flags: 00
  221. -- high flags: 4000
  222. -- rect: left=204 top=143 right=155 bottom=221
  223. -- title width / last selected line: 0
  224. -- icon id / first selected line: 0 / 0
  225. -- text alignment: 1
  226. -- font id: 0
  227. -- text size: 12
  228. -- style flags: 0
  229. -- line height: 16
  230. -- part name: foundIt
  231.  
  232.  
  233. -- part contents for card part 5
  234. ----- text -----
  235. FindInField version 1.4
  236. Roger Brown
  237.  
  238. The FindInField XFCN locates a string in a given field 
  239. or container as fast as HyperCard can.
  240.  
  241.  It searches only the specified container.  
  242.  
  243. You can specify a starting position and whether or not 
  244. the search is case sensitive.
  245.  
  246.  It returns information on the location of the find: 
  247.     item 1 is the byte position in the string (1-based)
  248.     item 2 is the line number in the string
  249.     item 3 is the byte position in the line (1-based)
  250.     item 4 is the item number in the line
  251.       --- all are zero if the string is not found
  252.    
  253.  
  254. INVOKING FindInField
  255.  
  256.   get FindInField(field,target,"true",start)
  257.  
  258.      where   field   is the name of the field or          
  259.                      container to search
  260.              target  is the string to search for
  261.             "true"   is an option parameter to indicate                 
  262.                      case sensitivity for the search
  263.              start   is the (optional) starting byte 
  264.                      offset
  265.                    
  266.  
  267. EXAMPLE
  268.  
  269.  ex.  get FindInField(card field 1,"EXAMPLE","true",0)
  270.  
  271.  
  272. REVISION HISTORY
  273. 1.1  4/31/88 - FindInField using HCLib was way too slow
  274. 1.2  5/5/88  - rewrite in more "C" style, one pass over 
  275.                the field
  276. 1.3  5/10/88 - add case sensitivity and starting 
  277.                position
  278. 1.4 7/21/88  - add item number output
  279.  
  280. -- part contents for card part 7
  281. ----- text -----
  282. /* FindInField1.4.c */
  283. /* ┬⌐ Digital Medicine,Inc. 1989 */
  284. /* written in LightSpeed C  ┬⌐ Think Technologies, Inc */
  285. /* by Roger Brown 7/21/88  Courseware Development group */
  286.  
  287. /* version 1.4 2/289 returns item number in line */
  288.  
  289. /* add start byte input as last parameter */
  290.  
  291. /* This is a HyperCard XFCN that locates a target string in an input
  292.    field (or other container) and returns the byte position in the field, the number 
  293.    of the line where it was find, and the byte position in that line.
  294.    Case sensitivity is used if an optional third parameter is "true".
  295.    An optional fourth parameter can be sent to specify the byte position
  296.    in the field where the search should start.
  297.     
  298.    Syntax is:
  299.    
  300.         get FindInField(container,target,"true",startByte)
  301.    
  302.    ex.      get FindInField(card field 1,"***")
  303.   
  304.    returns: item 1: byte offset in field or 0 if not found
  305.             item 2: line of card field 1 that contains '***' or 0
  306.             item 3: byte position in the line or 0
  307.             item 4: item number that contains it or 0
  308.    
  309.    where    container   is any hypercard container (field, variable), presumed to be 
  310.                         multi-lined.
  311.             target      is the string to look for   
  312.             true        is an optional parameter turning on case sensitivity   
  313.             startByte   is an optional parameter specifying the byte position
  314.                         in the container where the search should start.
  315.                                
  316.       To compile: create a project with this and MacTraps. Build as code resource type
  317.                   XFCN named FindInField.
  318. */
  319.  
  320.  
  321.  
  322. #include "stddata_ctype.c"
  323. #include "strings.c"
  324. #include "ToolboxUtil.h"
  325. #include "HyperXCmd.h"
  326. #include "XCmdGlue.inc.c"
  327. #include "SetUpA4.h"
  328.  
  329. /* change a string to all upper case */
  330.  
  331. int toupper(c)
  332. char    c;
  333. {
  334.     return( (c>='a')&&(c<='z') ? (c-('a'-'A')) : c );
  335. }
  336.  
  337. /* see if two characters match, maybe ignoring case */
  338.  
  339. matchChar(a,b,theCase)
  340. char a,b;
  341. int theCase;
  342. {
  343.     if (theCase==1) return (a==b);
  344.     return (toupper(a)==toupper(b));
  345. }
  346.  
  347. /* build the return result string from numeric parts */
  348.  
  349. BuildResult(fPos,fLine,lPos,item,result)
  350. int fPos,fLine,lPos,item;
  351. char *result;
  352. {
  353.     char temp[64];
  354.     
  355.     NumToString((long)fPos,temp);
  356.     PtoCstr((char*)temp);
  357.     strcpy(result,temp);
  358.     strcat(result,",");
  359.     NumToString((long)fLine,temp);
  360.     PtoCstr((char*)temp);
  361.     strcat(result,temp);
  362.     strcat(result,",");
  363.     NumToString((long)lPos,temp);
  364.     PtoCstr((char*)temp);
  365.     strcat(result,temp);
  366.     strcat(result,",");
  367.     NumToString((long)item,temp);
  368.     PtoCstr((char*)temp);
  369.     strcat(result,temp);
  370. }
  371.  
  372. /* find the target as specified */
  373.  
  374. FindIt(theField,theTarget,caseSens,result,start)
  375. char *theField;          /* pointer to field to search */
  376. char *theTarget;         /* pointer to target string */
  377. char *result;            /* pointer to the return result string */
  378. int caseSens,start;      /* case sensitive = 1 for yes, start is where (byte) to start */
  379. {
  380.     int c;             /* index of current character */
  381.     int tLen;          /* target length */
  382.     int line;          /* line currently being searched */
  383.     int lPos;          /* character position in line */
  384.     int item;          /* item number in line */
  385.     register char *fc,*tc; /* pointers to field and target characters */
  386.     
  387.     tLen = strlen(theTarget);         /* assume this is short */
  388.     c = lPos = 0; 
  389.     line = item = 1;                            /* offset into theField */
  390.     fc = theField;                       /* pointer into theField */
  391.     while (*fc != 0) {                   /* scan to end */
  392.         tc = theTarget;                  /* pointer to theTarget */
  393.         if (*fc==13) {
  394.              line++;                     /* passing a line boundary */
  395.              lPos = -1;                  /* reset position */
  396.              item = 1;                   /* reset item */
  397.         }
  398.         if (*fc==44) item++;
  399.         if ((c+1) > start) {             /* don't go until something matches */
  400.             while (matchChar(*tc,*fc,caseSens)) {   /* check for match */
  401.                 fc++;                      /* cycle through matching characters */
  402.                 tc++;
  403.                 if (tc == (theTarget+tLen)){   /* all target characters matched */
  404.                     BuildResult(c+1,line,lPos+1,item,result);
  405.                      return;  /* matched */
  406.                 }
  407.             }
  408.         }
  409.         c++;                          /* move along */
  410.         lPos++;
  411.         fc = theField + c;            /* reset pointer after a search */
  412.     }
  413.     BuildResult(0,0,0,0,result);
  414.     return;                         /* target not found */
  415. }
  416.  
  417. /* XCMD entry */
  418.  
  419. pascal void FindInField(paramPtr)
  420. XCmdBlockPtr    paramPtr;
  421. {   
  422.     int i;
  423.     Str255 theResult,caseStr,startStr;
  424.     Ptr theField,theTarget,casePtr,theStart;
  425.     long line;
  426.     Size len;
  427.     CursHandle theCursor;
  428.     Handle resultHandle;
  429.     int theCase;
  430.     long startAt;
  431.     
  432.     /* put up a watch */
  433.     
  434.     theCursor = GetCursor(watchCursor);
  435.     SetCursor(*theCursor);
  436.     
  437.     /* lock parameters down so we can point to themn */
  438.     
  439.     for (i=0;i<paramPtr->paramCount;i++) {
  440.         MoveHHi(paramPtr->params[i]);
  441.         HLock(paramPtr->params[i]);
  442.     }
  443.     
  444.     /* get the parameters */
  445.     
  446.     theField = *(paramPtr->params[0]);   /* the container */
  447.     theTarget = *(paramPtr->params[1]);  /* the target string */
  448.     if (paramPtr->paramCount>2)  {       /* case sensitivity flag */
  449.         casePtr = *(paramPtr->params[2]);
  450.         strcpy(caseStr,casePtr);
  451.     }
  452.     else strcpy(caseStr,"");
  453.     for (i=0;i<strlen(caseStr);i++) caseStr[i] = toupper(caseStr[i]);
  454.     if (strcmp(caseStr,"TRUE")==0) theCase = 1;
  455.     else theCase = 0;
  456.     
  457.     if (paramPtr->paramCount>3) {        /* start byte */
  458.         theStart = *(paramPtr->params[3]);
  459.         strcpy(startStr,theStart);
  460.         CtoPstr((char *)startStr);
  461.         StringToNum(startStr,&startAt);
  462.     }
  463.     else startAt = 0;
  464.        
  465.     /* do it */
  466.     
  467.     FindIt(theField,theTarget,theCase,theResult,(int)startAt);
  468.     
  469.     /* assemble the return string */
  470.     
  471.     len = 1+strlen(theResult);
  472.     resultHandle = NewHandle(len);
  473.     HLock(resultHandle);
  474.     BlockMove(theResult,*resultHandle,len);
  475.     HUnlock(resultHandle);
  476.     paramPtr->returnValue = resultHandle;
  477.     for (i=0;i<paramPtr->paramCount;i++) HUnlock (paramPtr->params[i]);
  478.     return;
  479. }
  480.  
  481. pascal void main(paramPtr)
  482. XCmdBlockPtr    paramPtr;
  483. /* this is the entry point for the XFCN */
  484. {
  485.         RememberA0();
  486.         SetUpA4();                  /* to access globals */
  487.         FindInField(paramPtr);      /* run the main event loop */  
  488.         RestoreA4(); 
  489.         return;
  490. }   
  491.  
  492.  
  493.  
  494. -- part contents for card part 9
  495. ----- text -----
  496. 210,9,31,1